<?php

namespace app\admin\controller;

use app\common\model\Order;
use app\common\model\OrderDetail;
use app\common\model\Shops;

class Orders extends Base
{
    public function __construct()
    {
        parent::__construct();
        $this->isLogin();
    }

    /**
     * @route('orders/shopsorder')
     * 商铺普通商品订单
     */
    public function shopsorder()
    {
        // 获取搜索条件
        $get['time'] = input('get.time');
        $get['pay_status'] = input('get.pay_status');
        $get['order_status'] = input('get.order_status');
        $get['shop_name'] = input('get.shop_name');
        $isdisplay = true;
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $ordersModel = new Order();

        $get['order_type'] = 1;
        $rs_data = $ordersModel->shops_order_list($get,'o.order_status asc,o.order_id desc',12,true);
//        echo "<pre>";
//        var_dump($rs_data);die;
        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
            "isdisplay" => $isdisplay,
            "statelist" => $ordersModel::$statelist,
            "paystatelist" => $ordersModel::$paystatelist,
        );
        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/shopslist');
    }

    /**
     * @route('orders/shiping')
     * 商铺普通商品订单
     */
    public function shiping()
    {
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
        }
        $get['order_id'] = input('get.order_id');
//		$get['order_type'] = 1;
        $ordersModel = new Order();
        $ExpressModel = new \app\common\model\Express();

        if(input('post.submit')){
            $res = $ordersModel->save([
                'order_status' => 3,
                'express_id'    => input('express_id'),
                'tracking_number' => input('post.tracking_number'),
                'shiping_time' => time(),
            ],$get);
            if(!$res){
                $this->error($ordersModel->getError());
            }
            $this->success('操作成功');
        }
        $data = Order::field('order_type,express_id,tracking_number')->get($get['order_id'])->toArray();
        if(!$data){
            $this->error('操作失败');
        }
        if($data['order_type'] != 1 && $data['order_type'] != 2 ){
            $this->error('该类型不允许发货');
        }
        $express_data = $ExpressModel->get_list(['status' => 1],'express_id asc','express_id,express_name');
        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            'order_id' => $get['order_id'],
            'express'   => $express_data,
            'data'  => $data
        );

        $this->assign("data", $tvar);
        return $this->fetch('orderslist/boxshiping');
    }

    /**
     * @route('orders/boxshopdetail')
     * 商铺订单详情
     */
    public function boxshoptdetail()
    {
        $order_id = input('get.order_id');
        $detailModel = new Order();
        $rs_data = $detailModel->shops_order_details($order_id);
//        var_dump($rs_data);die;
        // 后台账号数据
        $tvar = array(
            "title" => "商铺订单详情",
            "time"  => time(),
            "data"  => $rs_data,
        );
        $this->assign("data", $tvar);
        return $this->fetch('orderslist/boxshops');
    }


    /**
     * @route('orders/exchangeorder')
     * 兑换商品订单
     */
    public function exchangeorder()
    {
        // 获取搜索条件
        $get['time'] = input('get.time');
        $get['pay_status'] = input('get.pay_status');
        $get['order_status'] = input('get.order_status');
        $get['shop_name'] = input('get.shop_name');
        $isdisplay = true;
        //判断是总部还是商家
        if($this->_shopid != 0){//有shop_id的是商家
            $get['shop_id'] = $this->_shopid;
            $isdisplay = false;
        }

        $ordersModel = new Order();

        $get['order_types'] = 2;
        $rs_data = $ordersModel->shops_order_list($get,'o.order_id desc',$this->limit,true);

        $tvar = array(
            "title" => "商铺订单列表",
            "time"  => time(),
            "data"  => $rs_data['data'],
            "get"   => $get,
            "isdisplay" => $isdisplay,
            "statelist" => $ordersModel::$statelist,
            "paystatelist" => $ordersModel::$paystatelist,
        );

        $this->assign("data", $tvar);
        $this->assign('page', $rs_data['page']);
        return $this->fetch('orderslist/exchangelist');
    }

}